const redis = require('../config/redis');

class MockWeiboService {
  constructor() {
    this.cacheDuration = parseInt(process.env.CACHE_DURATION) || 300;
  }

  // 获取模拟微博热搜数据
  async getHotSearch() {
    const cacheKey = 'mock:weibo:hotsearch';
    
    return redis.getOrSet(cacheKey, async () => {
      console.log('Generating mock Weibo hot search data...');
      return this.generateMockData();
    }, this.cacheDuration);
  }

  // 生成模拟数据
  generateMockData() {
    const mockTopics = [
      {
        id: 'weibo_1',
        title: '#国庆节假期安排#',
        description: '国庆节放假通知发布',
        platform: '微博',
        views: 2856000,
        comments: 12500,
        heat: 95,
        trend: 12,
        updatedAt: new Date().toISOString(),
        url: 'https://s.weibo.com/weibo?q=%23国庆节假期安排%23',
        tags: ['热', '新'],
        rank: 1,
        icon: 'hot',
        label: '新',
        isAd: false,
        note: ''
      },
      {
        id: 'weibo_2',
        title: '#电影票房破纪录#',
        description: '国庆档电影票房创新高',
        platform: '微博',
        views: 1823000,
        comments: 8900,
        heat: 88,
        trend: 8,
        updatedAt: new Date().toISOString(),
        url: 'https://s.weibo.com/weibo?q=%23电影票房破纪录%23',
        tags: ['热'],
        rank: 2,
        icon: 'hot',
        label: '热',
        isAd: false,
        note: ''
      },
      {
        id: 'weibo_3',
        title: '#科技新品发布会#',
        description: '知名科技公司发布新产品',
        platform: '微博',
        views: 1568000,
        comments: 6700,
        heat: 82,
        trend: 5,
        updatedAt: new Date().toISOString(),
        url: 'https://s.weibo.com/weibo?q=%23科技新品发布会%23',
        tags: ['新'],
        rank: 3,
        icon: 'new',
        label: '新',
        isAd: false,
        note: ''
      },
      {
        id: 'weibo_4',
        title: '#体育赛事精彩瞬间#',
        description: '国际体育赛事精彩回顾',
        platform: '微博',
        views: 1345000,
        comments: 4500,
        heat: 76,
        trend: 3,
        updatedAt: new Date().toISOString(),
        url: 'https://s.weibo.com/weibo?q=%23体育赛事精彩瞬间%23',
        tags: ['沸'],
        rank: 4,
        icon: 'fei',
        label: '沸',
        isAd: false,
        note: ''
      },
      {
        id: 'weibo_5',
        title: '#旅游景点推荐#',
        description: '国庆假期旅游好去处',
        platform: '微博',
        views: 1123000,
        comments: 3800,
        heat: 71,
        trend: -2,
        updatedAt: new Date().toISOString(),
        url: 'https://s.weibo.com/weibo?q=%23旅游景点推荐%23',
        tags: ['推荐'],
        rank: 5,
        icon: '',
        label: '',
        isAd: true,
        note: '广告'
      }
    ];

    // 添加官方置顶
    const officialTopic = {
      id: 'weibo_gov_official',
      title: '#国家安全教育#',
      description: '提高国家安全意识，共建和谐社会',
      platform: '微博',
      views: 5000000,
      comments: 20000,
      heat: 99,
      trend: 0,
      updatedAt: new Date().toISOString(),
      url: 'https://s.weibo.com/weibo?q=%23国家安全教育%23',
      tags: ['官方', '置顶'],
      rank: 0,
      isOfficial: true,
      note: '官方发布'
    };

    return [officialTopic, ...mockTopics];
  }

  // 获取单个热点详情
  async getHotspotDetail(id) {
    const allData = await this.getHotSearch();
    const hotspot = allData.find(item => item.id === id);
    
    if (!hotspot) {
      throw new Error('热点不存在');
    }

    // 生成模拟趋势数据
    const trendData = this.generateTrendData(hotspot.heat);
    
    return {
      hotspot,
      trendData
    };
  }

  // 生成趋势数据
  generateTrendData(baseHeat) {
    const now = new Date();
    const data = [];
    
    for (let i = 11; i >= 0; i--) {
      const time = new Date(now.getTime() - i * 2 * 60 * 60 * 1000);
      const hour = time.getHours().toString().padStart(2, '0');
      const minute = '00';
      
      // 模拟热度波动
      const fluctuation = Math.random() * 10 - 5;
      const heat = Math.max(10, Math.min(100, baseHeat + fluctuation));
      
      data.push({
        time: `${hour}:${minute}`,
        heat: Math.round(heat)
      });
    }
    
    return data;
  }
}

module.exports = new MockWeiboService();